﻿using System;
using UnityEngine;
using UnityEngine.AI;
using XUtliPoolLib;

namespace XMainClient
{
	// Token: 0x02000AFE RID: 2814
	internal class XAIMove : XSingleton<XAIMove>
	{
		// Token: 0x0600A600 RID: 42496 RVA: 0x001CFAE4 File Offset: 0x001CDCE4
		public bool NavToTarget(XEntity host, Vector3 oTargetPos, float speed)
		{
			bool flag = host == null || host.Nav == null;
			return !flag && this.NavToPos(host, oTargetPos, speed);
		}

		// Token: 0x0600A601 RID: 42497 RVA: 0x001CFB18 File Offset: 0x001CDD18
		public bool NavToPos(XEntity host, Vector3 targetpos, float speed)
		{
			if ((host.EngineObject.Position - targetpos).magnitude <= 2f)
			{
				Vector3 vector = host.EngineObject.Position - targetpos;
				vector.y = 0f;
				host.Net.ReportMoveAction(targetpos, speed, false, false, false, XSingleton<XCommon>.singleton.AngleToFloat(-vector));
			}
			else
			{
				NavMeshHit navMeshHit;
				if (!NavMesh.SamplePosition(host.EngineObject.Position, out navMeshHit, 1f, -1))
				{
					host.Net.ReportMoveAction(targetpos, speed, false, false, false, XSingleton<XCommon>.singleton.AngleToFloat(targetpos - host.EngineObject.Position));
				}
				else
				{
					XNavigationEventArgs @event = XEventPool<XNavigationEventArgs>.GetEvent();
					@event.Firer = host;
					@event.Dest = targetpos;
					@event.CameraFollow = false;
					XSingleton<XEventMgr>.singleton.FireEvent(@event);
					if (!host.Nav.IsOnNav)
					{
						@event.Dest = targetpos + (host.EngineObject.Position - targetpos).normalized * 2f;
						XSingleton<XEventMgr>.singleton.FireEvent(@event);
						if (!host.Nav.IsOnNav)
						{
							host.Net.ReportMoveAction(targetpos, speed, false, false, false, XSingleton<XCommon>.singleton.AngleToFloat(targetpos - host.EngineObject.Position));
						}
					}
				}
			}
			return true;
		}

		// Token: 0x0600A602 RID: 42498 RVA: 0x001CFCB8 File Offset: 0x001CDEB8
		public bool RotateToTarget(XEntity entity, XEntity target)
		{
			Vector3 position = target.EngineObject.Position;
			Vector3 position2 = entity.EngineObject.Position;
			Vector3 dir = position - position2;
			float magnitude = dir.magnitude;
			entity.Net.ReportRotateAction(dir);
			return true;
		}

		// Token: 0x0600A603 RID: 42499 RVA: 0x001CFD04 File Offset: 0x001CDF04
		public bool FindNavPath(XEntity entity)
		{
			return entity.AI.RefreshNavTarget();
		}

		// Token: 0x0600A604 RID: 42500 RVA: 0x001CFD24 File Offset: 0x001CDF24
		public bool ActionMove(XEntity entity, Vector3 dir, Vector3 dest, float speed)
		{
			if (entity.Nav != null)
			{
				this.ActionNav(entity, dest, speed);
			}
			else
			{
				if (entity.Fly != null)
				{
					dest.y = entity.Fly.CurrentHeight + XSingleton<XScene>.singleton.TerrainY(dest);
				}
				entity.Net.ReportMoveAction(dest, speed * entity.AI.MoveSpeed, false, false, false, 0f);
				XSecurityStatistics xsecurityStatistics = XSecurityStatistics.TryGetStatistics(entity);
			}
			return true;
		}

		// Token: 0x0600A605 RID: 42501 RVA: 0x001CFDAC File Offset: 0x001CDFAC
		public bool ActionNav(XEntity entity, Vector3 dest, float speedRatio = 1f)
		{
			XNavigationEventArgs @event = XEventPool<XNavigationEventArgs>.GetEvent();
			@event.Firer = entity;
			@event.Dest = dest;
			@event.CameraFollow = false;
			@event.SpeedRatio = speedRatio;
			return XSingleton<XEventMgr>.singleton.FireEvent(@event);
		}

		// Token: 0x0600A606 RID: 42502 RVA: 0x001CFDEC File Offset: 0x001CDFEC
		public bool ActionRotate(XEntity entity, float degree, float speed, int type)
		{
			Vector3 dir = Quaternion.Euler(new Vector3(0f, degree, 0f)) * ((type == 0) ? entity.EngineObject.Forward : Vector3.forward);
			entity.Net.ReportRotateAction(dir, speed, 0L);
			return true;
		}

		// Token: 0x0600A607 RID: 42503 RVA: 0x001CFE40 File Offset: 0x001CE040
		public bool RotateToTarget(XEntity entity)
		{
			if (entity.AI.Target == null)
			{
				return false;
			}
			entity.Net.ReportRotateAction(entity.AI.Target.EngineObject.Position - entity.EngineObject.Position, entity.Attributes.RotateSpeed, 0L);
			return true;
		}

		// Token: 0x0600A608 RID: 42504 RVA: 0x001CFEA8 File Offset: 0x001CE0A8
		public bool UpdateNavigation(XEntity entity, int dir, int oldDir)
		{
			Vector3 vector = entity.AI.Patrol.GetCurNavigationPoint();
			if (vector == Vector3.zero)
			{
				return false;
			}

			if (dir != oldDir)
			{
				entity.AI.Patrol.ToggleNavDir();
				vector = entity.AI.Patrol.GetNextNavPos();
			}
			else if ((entity.EngineObject.Position - vector).magnitude <= 0.5f || entity.AI.Patrol.IsInNavGap)
            {
                if (entity.AI.Patrol.IsInNavGap)
                {
                    float num = Time.realtimeSinceStartup - entity.AI.Patrol.NavNodeFinishTime;
                    if (num < entity.AI.Patrol.GetCurNavGap())
                    {
                        return true;
                    }
                    vector = entity.AI.Patrol.GetNextNavPos();
                }
                else
                {
                    if (entity.AI.Patrol.GetCurNavGap() > 0f)
                    {
                        entity.AI.Patrol.NavNodeFinishTime = Time.realtimeSinceStartup;
                        entity.AI.Patrol.IsInNavGap = true;
                        return true;
                    }
                    vector = entity.AI.Patrol.GetNextNavPos();
                }
            }
            entity.AI.Patrol.IsInNavGap = false;
			//TODO
			//(entity.EngineObject.Position - vector).y = 0f;
			this.NavToPos(entity, vector, entity.AI.MoveSpeed);
			return true;
		}

		// Token: 0x0600A609 RID: 42505 RVA: 0x001D005C File Offset: 0x001CE25C
		public bool IsPointInMap(Vector3 pos)
		{
			return XSingleton<XScene>.singleton.IsWalkable(pos);
		}

		// Token: 0x04003D03 RID: 15619
		private Vector3 _arena_center = Vector3.zero;
	}
}
